package com.zhiwang.xiaoqu.ui.withdraw


import android.content.Context
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.recyclerview.widget.GridLayoutManager

import com.zhiwang.xiaoqu.R
import com.zhiwang.xiaoqu.di.component.DaggerUserComponent
import com.zhiwang.xiaoqu.di.module.AutoDisposeModule
import com.zhiwang.xiaoqu.mvp.model.response.WithDrawItem
import com.zhiwang.xiaoqu.mvp.presenter.UserPresenter
import com.zhiwang.xiaoqu.ui.BaseFragment
import com.zhiwang.xiaoqu.widget.Toasty
import kotlinx.android.synthetic.main.bind_weixin_layout.*
import kotlinx.android.synthetic.main.fragment_wei_xin.*
import kotlinx.android.synthetic.main.recyclerview.*
import me.drakeet.multitype.MultiTypeAdapter
import javax.inject.Inject

/**
 * A simple [Fragment] subclass.
 */
class WeiXinFragment : BaseFragment(), View.OnClickListener {

    @Inject
    lateinit var userPresenter: UserPresenter

    var isBind = false

    var count = 0

    lateinit var withDrawList: List<WithDrawItem>

    var fragmentInteractListener: WithDrawFragmentInteractListener? = null

    private val withDrawViewBinder = WithDrawItemViewBinder()

    override fun onAttach(context: Context) {
        super.onAttach(context)
        if (context is WithDrawFragmentInteractListener)
            fragmentInteractListener = context
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_wei_xin, container, false)
    }


    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        load()
    }

    fun load() {
        if (isBind.not()) {
            wx_bind_layout.visibility = View.GONE
            bindWXViewStub.inflate()
            tv_copyBindCode.setOnClickListener {
                Toast.makeText(requireContext(), "复制绑定码", Toast.LENGTH_SHORT).show()
            }

            tv_copyGzh.setOnClickListener {
                Toast.makeText(requireContext(), "复制公众号", Toast.LENGTH_SHORT).show()
            }
        } else {
            wx_bind_layout.visibility = View.VISIBLE
            view?.findViewById<View>(R.id.wx_not_bind_layout)?.visibility = View.GONE
            userPresenter.getWeiXinWithDrawList(::onLoadWithDrawList, ::onError)

            tv_withdraw_btn.setOnClickListener(this)
        }
    }

    override fun onClick(v: View?) {
        when (v?.id) {

            R.id.tv_withdraw_btn -> {
                val num = withDrawList[withDrawViewBinder.curr].num
                if (fragmentInteractListener?.checkBalanceWithDrawEnabled(num = num.toInt()) != false)
                    userPresenter.withdrawWeiXin(num, onSuccess = ::onWithDrawSuccess, onError = ::onError)
                else{
                    Toasty.message(getString(R.string.balance_not_enough))
                }
            }
        }
    }

    fun onWithDrawSuccess(msg: String) {

    }

    private fun onLoadWithDrawList(list: List<WithDrawItem>) {
        withDrawList = list
        recyclerView.layoutManager = GridLayoutManager(context, 3)
        val typeAdapter = MultiTypeAdapter()
        typeAdapter.register(WithDrawItem::class.java, withDrawViewBinder)
        recyclerView.adapter = typeAdapter
        typeAdapter.items = list
    }

    override fun onHiddenChanged(hidden: Boolean) {
        super.onHiddenChanged(hidden)
        //todo 模拟从未绑定到已经绑定的过程
        count++
        if (count >= 2) {
            isBind = true
            load()
        }
    }


}
